<? if(!$treeData): ?>
<div class="tree-controls">
	<a href="#" class="none" title="Zatvori sve"></a>
	<a href="#" class="all" title="Otvori sve"></a>
</div>
<? endif; ?>
<?
	if(!isset($treeData)){
		$treeData = $this->dbCategory->tree();
	}
?>
<ul class="tree">
	<? foreach($treeData as $k=>$row): ?>
	
	<? 
		$class = "";
		$class .= $k==0 ? " first" : "";
		$class .= $k==count($treeData)-1 ? " last" : "";
	?>
	
	<li class="<?= $row["children"] ? "submenu" : "" ?><?= $class ?>" value="<?= $row["id"] ?>">
		<input type="hidden" name="position[<?= $row["id"] ?>]" value="<?= $row["position"]; ?>" />
		<? if($row["children"]): ?>
			<a href="javascript:void(0)" class="toggle closed"></a>
		<? else: ?>
			<span class="spacer"></span>
		<? endif; ?>
		<label class="radio<?= $value==$row["id"] ? " selected" : "" ?>">
			<?= $row["name"]; ?>
			<input type="radio" style="display:none" name="<?= $name ?>" value="<?= $row["id"] ?>"<?= $value==$row["id"] ? " checked=\"checked\"" : "" ?> />
		</label>
		<? if($row["children"]): ?>
			<?= $this->fetch("elements/".basename(__FILE__, ".ctp"), array("treeData"=>$row["children"], "name"=>$name, "value"=>$value)) ?>
		<? endif; ?>
	</li>
	<? endforeach; ?>
</ul>

<script type="text/javascript">
$(function(){
	var ul = openSelected(ul);
	do{
		ul.parent().children(".toggle").removeClass("closed").addClass("opened");
		ul.show();
		ul = ul.parent().parent();
	}while(ul.parent().hasClass("submenu"));
	
	function openSelected(){
		var inpt = $('input[value=<?= $value ?>]');
		var li = inpt.parent().parent();
		var ul = li.parent();
		var a = ul.parent().children("a.toggle");
		if(a.length){
			a.removeClass("closed").addClass("opened");
			ul.show();
		}
		return a.parent().parent();
	}
});
</script>